# coding: utf-8
'''
Description: 
version: 1.0.0
Author: peng2.zhang
Date: 2022-03-16 16:43:11
LastEditors: peng2.zhang
LastEditTime: 2022-03-16 17:13:43
'''

import re
import gitlab
from urllib.parse import urlparse
from urllib.parse import urljoin
import requests
from configparser import ConfigParser

class GitParser(object):
    url = "https://git.ecarxgroup.com"
    # token = "V6w6ocyhWTN8gax1q6aw"

    def __init__(self, job_name, build_id):
        self.job_name = job_name
        self.build_id = build_id
        self.product = self.job_name.split("_")[0]
        self.url = f"http://ecarx.stor.ecarx.com.cn/autobuildtestline/git/{self.product}/{self.job_name}/{self.build_id}/git_commit_message.properties"
        self.config = self.load_config()

    def load_config(self):
        res = requests.get(self.url)
        if res.status_code == 200:
            config_string = res.text
            parser = ConfigParser()
            parser.read_string("[DEFAULT]\n" + config_string)
            res = parser.items('DEFAULT')
            config_dict = dict(parser.items('DEFAULT'))
        else:
            config_dict = {}
        return config_dict
    
    @property
    def git_commit_branch(self):
        _git_branch_name = self.config.get("git_branch_name", "master")
        return _git_branch_name
    
    @property
    def git_commit_user(self):
        _git_commit_user = self.config.get("git_commit_user", "")
        return _git_commit_user
    
    @property
    def git_commit_id(self):
        _git_commit_id = self.config.get("git_commit_id", "")
        return _git_commit_id
    
    @property
    def git_commit_time(self):
        _git_commit_time = self.config.get("git_commit_time", "1999-01-01 00:00:00")
        return _git_commit_time
